package cn.cloudreal.cbms.budget.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import cn.cloudreal.cbms.budget.service.BudgetControlService;
import cn.cloudreal.cbms.common.util.Arith;
import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.DatadicContext;
import cn.cloudreal.cbms.common.util.StringUtils;
import cn.cloudreal.cbms.vo.BudgetControl;
import cn.cloudreal.cbms.vo.Dictionary;
import cn.cloudreal.cbms.vo.UserInfo;

import com.opensymphony.xwork2.ActionSupport;

public class BudgetControlAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	
	 private static final Logger log = Logger.getLogger(BudgetControlAction.class);
	 
	 /** 服务 */
	 BudgetControlService budgetControlService;
	 
    /** 计提截止时间 */
	private String sourcePutEndTime = null;
	
	String toScheduleMon = null;
	/** 归属地市 */
	private String accrualCity;  
	
    /** 归属地市下拉 */
    private List<Dictionary> dataListCITY = null;
	
	/**资源投放总表*/
    List<BudgetControl> budgetControls;
    
    BudgetControl totalBudgetControl;

	//获取资源投放总量列表
    public String budgetControlList(){
    	// 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter BudgetControlAction budgetControlList() method, return String");
        }
        
        try
        {
        getDictionary();  
    	if (StringUtils.isEmpty(sourcePutEndTime)) 
    	{
    		sourcePutEndTime = StringUtils.formatDateToString(new Date(), "yyyy-MM");
        }
    	// 获取当前session对象中所属城市
        UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                .getSession()
                .getAttribute(Constants.SESSIONPRKEY);
//        userInfo.getUserRole().setRoleRange("2");
//        userInfo.setUsercity("FS");
     // 非空判断 
        if (null != userInfo && accrualCity == null){
        	// 默认东莞
   		 	accrualCity = "DG";
        	 if (userInfo.getUserRole() != null){
        		 
        		 //非空重置
        		 if( null != userInfo.getUsercity()){
        			 if (!"".equals(userInfo.getUsercity()))
        			 {
        				 accrualCity = userInfo.getUsercity();
        			 }
        		 }
    
        	//操作范围
        	String roleRange = userInfo.getUserRole().getRoleRange();
        	if (roleRange!= null){
	        	//省级
        		 // 地市级
                if ("2".endsWith(roleRange)||"3".endsWith(roleRange))
                {
					dataListCITY.clear();
					Dictionary dictionary = (Dictionary) DatadicContext
							.getInstance().getDatadicObject(accrualCity,
									"CITY");
					dataListCITY.add(dictionary);

                }
            }
        }
    }
	//查询
		Map condition1 = new HashMap();
        condition1.put("i_month", sourcePutEndTime + "-01");
        condition1.put("i_city", accrualCity);
        budgetControls = budgetControlService.getBudgetDetaillList(condition1);
        
        Double toPlanAmountTotal = 0d;
        Double toFundAmountTotal = 0d;
        Double toPlanAmountNow = 0d;
        Double toFundAmountNow = 0d;
        Double toJtAmount = 0d;
        Double toPlanAmountTotal2 = 0d;
        
		// 计算总计行	        
        for(int i=0; i < budgetControls.size(); i++){
        	
        	toPlanAmountTotal  = Arith.add(toPlanAmountTotal, budgetControls.get(i).getPlanAmountTotal());
        	toFundAmountTotal = Arith.add(toFundAmountTotal, budgetControls.get(i).getFundAmountTotal());
        	toPlanAmountNow = Arith.add(toPlanAmountNow, budgetControls.get(i).getPlanAmountNow());
        	toFundAmountNow = Arith.add(toFundAmountNow, budgetControls.get(i).getFundAmountNow());
        	toJtAmount = Arith.add(toJtAmount, budgetControls.get(i).getJtAmount());
        	toPlanAmountTotal2 = Arith.add(toPlanAmountTotal2, budgetControls.get(i).getPlanAmountTotal2());
         }
        Double toFundRatioTotal = computeToFRatioTotal(toFundAmountNow,toPlanAmountTotal);
        Double toJtAmountRatioTotal = computeToJtRatioTotal(toJtAmount,toPlanAmountTotal);
        Double toStandardRatio = computeToStandardRatio(toPlanAmountTotal,toPlanAmountTotal2);
        toScheduleMon = getJindu(toFundAmountTotal,toPlanAmountTotal);
        
        totalBudgetControl = new BudgetControl();
        totalBudgetControl.setPlanAmountTotal(toPlanAmountTotal);
        totalBudgetControl.setFundAmountTotal(toFundAmountTotal);
        totalBudgetControl.setPlanAmountNow(toPlanAmountNow);
        totalBudgetControl.setFundAmountNow(toFundAmountNow);
        totalBudgetControl.setJtAmount(toJtAmount);
        totalBudgetControl.setFundRatioTotal(toFundRatioTotal);
        totalBudgetControl.setJtAmountRatioTotal(toJtAmountRatioTotal);
        totalBudgetControl.setStandardRatio(toStandardRatio);
        totalBudgetControl.setScheduleMon(toScheduleMon);
	        	
        // 重置返回
	      resultName = SUCCESS;
			
	}catch (Exception e)
	 {
	        // 打印异常信息
	        e.printStackTrace();
	        // 打印异常方法体日志
	        if (log.isInfoEnabled())
	        {
	            log.error("Exception BudgetControlAction budgetControlList() method",
	                    e);
	        }
	        // 重置返回值
	        resultName = ERROR;
	    }

	    // 打印退出方法体日志
	    if (log.isInfoEnabled())
	    {
	        log.info("Exit BudgetControlAction budgetControlList() method, return String");
	    }
	    
	    return resultName;
	}


    public Double computeToFRatioTotal(Double toF,Double toP){
    	if(toP == 0){
    		return 0d;
    	}
    	return Arith.div(toF, toP, 2);
    }
    public Double computeToJtRatioTotal(Double toJt,Double toP){
    	if(toP == 0){
    		return 0d;
    	}
    	return Arith.div(toJt, toP, 2);
    }
    public Double computeToStandardRatio(Double toP1,Double toP2){
    	if(toP2 == 0){
    		return 0d;
    	}
    	return Arith.div(toP1, toP2, 2);
    }
    public String getJindu(Double v1,Double v2){
    	if (v1 > v2)
    	{toScheduleMon = "大于进度";}
    	else if(v1 < v2)
    	{toScheduleMon = "小于进度";}
    	else{toScheduleMon = "等于进度";}
    	return toScheduleMon;
    	}
    
  //初始化归属地
	public void getDictionary() {
		dataListCITY = DatadicContext.getInstance().getDicDataListByCode("CITY");// 归属地市
	}
	
    public BudgetControl getTotalBudgetControl() {
		return totalBudgetControl;
	}

	public void setTotalBudgetControl(BudgetControl totalBudgetControl) {
		this.totalBudgetControl = totalBudgetControl;
	}

	public List<BudgetControl> getBudgetControls() {
		return budgetControls;
	}


	public void setBudgetControls(List<BudgetControl> budgetControls) {
		this.budgetControls = budgetControls;
	}
	
	 public BudgetControlService getBudgetControlService() {
		return budgetControlService;
	}

	public void setBudgetControlService(BudgetControlService budgetControlService) {
		this.budgetControlService = budgetControlService;
	}
		
	public String getSourcePutEndTime() {
		return sourcePutEndTime;
	}

	public void setSourcePutEndTime(String sourcePutEndTime) {
		this.sourcePutEndTime = sourcePutEndTime;
	}

	public String getAccrualCity() {
		return accrualCity;
	}

	public void setAccrualCity(String accrualCity) {
		this.accrualCity = accrualCity;
	}

	public List<Dictionary> getDataListCITY() {
		return dataListCITY;
	}

	public void setDataListCITY(List<Dictionary> dataListCITY) {
		this.dataListCITY = dataListCITY;
	}
}
